﻿/*
Copyright (C) 2012 Laurent Tardif (laurent.tardif@persistentsas.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using org.codeplex.TestUtils;


namespace IQToolsFonctionalTests
{
    [Binding]
    public class StepDefinitionsIQTools
    {
        String generatedFile = null;
        //String referenceFile = null;
        String configurationFile = null; 

        [Given(@"I have a server with powershell installed")]
        public void GivenIHaveAServerWithPorwershellInstalled()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0");
            Assert.IsTrue(dirInfo.Exists) ;
            FileInfo fileInfo = new FileInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe");
            Assert.IsTrue(fileInfo.Exists);
            //TODO : check file version > 1.0 ? 

        }

        [Given(@"I have given an XML outputfile named (.*)")]
        public void GivenIHaveGivenAnOutputfileNamed(string filename)
        {
            String dir = Directory.GetCurrentDirectory();
            generatedFile = Path.Combine(dir, filename);
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsFalse(File.Exists(generatedFile), "the file " + generatedFile + " should not exist");
        }

        [Given(@"I have provided a correct (.*) file named (.*) with (.*) asked")]
        public void GivenIHaveProvidedACorrectFileWithOnlySoftwareInformationsAsked(string filetype, string filename, string elements)
        {
            String dir = Directory.GetCurrentDirectory();
            configurationFile = Path.Combine(dir, filename);
            Console.WriteLine("Will work with configuration file " + configurationFile);
            Assert.IsTrue(File.Exists(configurationFile), "the file " + configurationFile + " should exist");
        }

        [Then(@"the resulting file should contain only (.*) (.*) section")]
        public void ThenTheResultingFileShouldContainsOnlySection(int number, string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            Boolean check = StringUtils.CheckExactOccurence(content, "<" + sectionName, number);
            Assert.IsTrue(check, "File should contains a " + sectionName + " section with "+number+" element(s)");
        }

        [Then(@"the resulting file should contain a (.*) section")]
        public void ThenTheResultingFileShouldContainsSection(string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            Boolean check = StringUtils.CheckContains(content, "<" + sectionName);
            Assert.IsTrue(check, "File should contains a "+sectionName+" section");
        }

          [Then(@"the resulting file should contain a software named (.*)")]
          public void ThenTheResultingFileShouldContainASoftwareNamed(string programName)
          {
              Console.WriteLine("Will work with result file " + generatedFile);
              Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
              String content = File.ReadAllText(generatedFile);
              Boolean check = StringUtils.CheckContains(content, "<name>" + programName);
              Assert.IsTrue(check, "File should contains a " + programName + " program");
          }


        
        [Then(@"the resulting file should not contain a (.*) section")]
        public void ThenTheResultingFileShouldNotContainsASoftwareSection(string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            //match softwares and softaware 
            Boolean check = StringUtils.CheckDoNotContains(content, "<" + sectionName);
            Assert.IsTrue(check, "File contains a "+sectionName+" section");
        }

        [When(@"I run the IQTOOLS script")]
        public void WhenIRunTheIQTOOLSScript()
        {
            Console.WriteLine("start success");
            Console.WriteLine("IQTools.ps1 -configFile " + configurationFile + " -outputFile " + generatedFile + " ");
            int errorCode = ShellUtils.ExecutePowershellScript("IQTools.ps1", @"-configFile " + configurationFile + " -outputFile " + generatedFile + " ", 50000);

            Assert.AreEqual(0, errorCode, "The return code should be = 0 as all parameters are valid (directory)");
            Console.WriteLine("run success");
        }

    }

}
